import {BaseObject} from "../model/baseDataModel/baseObject.js";
import {ProductDataModel} from "../model/baseDataModel/productDataModel.js";

/**
 * @description 基础模型控制器
 * @extends {BaseObject} 基础对象
 * @class {BaseController}
 * @author hhx
 * @date 2024-11-22 13:11
 * @version 0.1.2
 */
export class BaseController extends BaseObject {

    /**
     * @description 基础控制器
     * @public
     * @constructor
     * @param prop {object}
     */
    constructor(prop) {

        super(prop);

    }

    //========================== 内部成员 ==========================

    get view() {
        return this.properties.get('view');
    }

    set view(view) {
        this.properties.set('view', view);
    }

    get model() {
        return this.properties.get('model');
    }

    set model(model) {
        this.properties.set('model', model);
    }

    get pubsub() {
        return this.properties.get('pubsub');
    }

    set pubsub(model) {
        this.properties.set('pubsub', model);
    }

    //========================== 成员方法 ==========================

    getView() {
        return this.view;
    }

    setView(view) {
        this.view = view;
    }

    getModel() {
        return this.model;
    }

    setModel(model) {
        this.model = model;
    }

    getPubsub() {
        return this.pubsub;
    }

    setPubsub(pubsub) {
        this.pubsub = pubsub;
    }

    //========================== 内部方法 ==========================

    init(prop) {

        super.init(prop);

        this.bindEvent('#btn1', 'click', this.renderList.bind(this))
    }

    bindEvent(element, binding, callback) {
        let el = document.querySelector(element);
        el.addEventListener(binding, callback)
    }

    renderList(e) {
        let product = new ProductDataModel({
            productId: 1,
            productName: '裤子',
            price: 12,
            description: '值得推荐',
            stock: 15
        })
        this.model.addProduct(product);
        this.pubsub.publish('change-product', this.model.productList);
    }


}